﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using _MetalSlug.MyCharacters.Characters;
using _MetalSlug.Soldiers.Units;
using _MetalSlug.Form.ScreenSystem;
using _MetalSlug.Form;
using _MetalSlug.Form.ScreenSystem.Message;
using _MetalSlug.Mission;

namespace _MetalSlug.Weapons.Units
{
    public class MiniGunBullet : Bullet
    {
        private List<Texture2D> miniBullet;
        private List<Texture2D> sgBullet;

        public MiniGunBullet()
        {
        }

        public MiniGunBullet(Vector2 topleft, Vector2 size, Vector2 _Speed)
        {
            Type = TYPE_BULLET.MINIGUN_BULLET;
            InitialAttribute(topleft,size, _Speed);
        }

        public override void LoadContent()
        {
            List<Texture2D> Textures = GameUtil.LoadTextures("Weapon/MiniGunBullet/",1);
            MySprite temp = new MySprite(Textures, TopLeft);
            temp.Size = Size;
            Sprites.Add(temp);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Sprites[0].TopLeft = TopLeft; 
            if (Visible == true)
            {
                base.Draw(spriteBatch);
            }         
        }

        public override void Fire(Vector2 theStartPosition, Vector2 theDirection)
        {
            if (theDirection == -Vector2.UnitX)
            {
                TopLeft = new Vector2(theStartPosition.X - 30, theStartPosition.Y + 95);
                Sprites[0].Flip = MySprite.FLIPABLE.FLIP;
                Sprites[0].Rotation = 0;
            }

            if (theDirection == Vector2.UnitX)
            {
                TopLeft = new Vector2(theStartPosition.X + 120, theStartPosition.Y + 95);
                Sprites[0].Flip = MySprite.FLIPABLE.NOT_FLIP;
                Sprites[0].Rotation = 0;
            }
           
            if (theDirection == -Vector2.UnitY)
            {
                TopLeft = new Vector2(theStartPosition.X+70 , theStartPosition.Y -10);
                Sprites[0].Rotation = 1.5f;
                Sprites[0].Flip = MySprite.FLIPABLE.FLIP;
            }

            StartPosition = theStartPosition;
            Direction = theDirection;
        }

        private float time = 0;
        public override void Update(GameTime gameTime, List<Enemy> enemies)
        {
            foreach (Enemy enemy in enemies)
            {
                if(!enemy.IsDead)
                {
                    if ((TopLeft.X >= enemy.TopLeft.X) && (TopLeft.X <= enemy.TopLeft.X + 100)&&
                        (TopLeft.Y >= enemy.TopLeft.Y) && (TopLeft.Y <= enemy.TopLeft.Y + 180))     
                    {
                        enemy.Hp -= 10;
                        MyMessage sendMessage = new AddGameCoreMessage(10);
                        GameManager.GetInstance().Receive(null, MyScreen.TYPE.PLAYGAME, sendMessage);
                        //enemy.Sprites[0].Color = Color.Red; 
                        TopLeft = Vector2.Zero;
                        Visible = false;
                        break;
                    }      
                }
            }

            time++; 
            if (Vector2.Distance(StartPosition, TopLeft) > 500 )
            {
                Visible = false;
            }

            if (Visible == true)
            {
                TopLeft +=  Direction * Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
   
        }

        public override void Update(GameTime gameTime, MyMap map, MyCharacter Hero)
        {

            if ((TopLeft.X >= Hero.TopLeft.X) && (TopLeft.X <= Hero.TopLeft.X + 100) &&
                    (TopLeft.Y >= Hero.TopLeft.Y) && (TopLeft.Y <= Hero.TopLeft.Y + 180))
            {
                Hero.Hp -= 200;
                Visible = false;
            }

            if (Vector2.Distance(StartPosition, TopLeft) > 800)
            {
                Visible = false;

            }

            if (Visible == true)
            {
                TopLeft += Direction * Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                Sprites[0].nextFrame();
            }
            time++;
        }

        public override Bullet Clone()
        {
            Bullet miniGunBullet = new MiniGunBullet();
            miniGunBullet.Copy(this);
            return miniGunBullet;
        }

        public override void Copy(Bullet Origin)
        {
            base.Copy(Origin);
            MiniGunBullet miniGunBullet = (MiniGunBullet)Origin;
            miniBullet = miniGunBullet.miniBullet;
            sgBullet = miniGunBullet.sgBullet;
        }
    }
}
